define(
	[
		'require',
		'../js/cytoscape.js',
		'../js/cytoscape-cose-bilkent.js'
	],
	function(require,cytoscape,regCose){
		regCose( cytoscape );
		var cycleIsEnd = 1;
		var Controller = Ember.Controller.extend({
			needs: ["serviceServer"],
			keyUpEnent: function(){
				var ctrl = this;
				setTimeout(function(){
					$("#mouseKeyUp").on("keyup",function(event){
						if(event.keyCode == 13){
							var data = ctrl.get('model');
							ctrl.topoDataRender(data);
						};
					});
				},0)
			},
			topoDataRender:function(data) {
				var IP = this.get('topoIP');

				var topoData = {
				    nodes: [],
				    edges: []
				}

				var nodeIsExist = {};
				var edgeIsExist = {};

				if(!IP){
					data.relations.forEach(function(item) {
					    if(item.from&&!nodeIsExist[item.from.id]){
					    	topoData.nodes.push({
		                        data: {
		                            id: item.from.id,
		                            name: item.from.name
		                        }
		                    });
		                    nodeIsExist[item.from.id] = item.from.id;
					    }
					    if(item.to&&!nodeIsExist[item.to.id]){
					    	topoData.nodes.push({
		                        data: {
		                            id: item.to.id,
		                            name: item.to.name
		                        }
		                    });
		                    nodeIsExist[item.to.id] = item.to.id;
					    }
					});

					data.relations.forEach(function(item,index) {
						if(item.from&&item.to){

					    	topoData.edges.push({
		                        data: {
		                            id: item.from.id + item.to.id,
		                            source: item.from.id,
		                            target: item.to.id
		                        }
		                    });
					    }
					});
				}else{
					data.relations.forEach(function(item,index) {
					    if(item.from&&item.from.name&&item.from.name.indexOf(IP)>-1){
					    	if(!nodeIsExist[item.from.id]){
						    	topoData.nodes.push({
			                        data: {
			                            id: item.from.id,
			                            name: item.from.name
			                        }
			                    });
					    	}
		                    item.from.isNeed = true;
		                    nodeIsExist[item.from.id] = item.from.id;
					    }
					    if(item.to&&item.to.name&&item.to.name.indexOf(IP)>-1){
					    	if(!nodeIsExist[item.to.id]){
						    	topoData.nodes.push({
			                        data: {
			                            id: item.to.id,
			                            name: item.to.name
			                        }
			                    });
					    	}
		                    item.to.isNeed = true;
		                    nodeIsExist[item.to.id] = item.to.id;
					    }
					});

					while(cycleIsEnd){
						cycleIsEnd = 0;
						data.relations.forEach(function(item,index) {
							if(item.from&&item.to){
						    	if(item.from.isNeed||item.to.isNeed){
						    		if(!edgeIsExist[item.from.id + item.to.id]){
								    	topoData.edges.push({
					                        data: {
					                            id: item.from.id + item.to.id,
					                            source: item.from.id,
					                            target: item.to.id
					                        }
					                    });
								    	edgeIsExist[item.from.id + item.to.id] = item.from.id + item.to.id;
									    cycleIsEnd++;

					                    if(!nodeIsExist[item.from.id]){
					                    	topoData.nodes.push({
						                        data: {
						                            id: item.from.id,
						                            name: item.from.name
						                        }
						                    });
						                    item.from.isNeed = true;
						                    nodeIsExist[item.from.id] = item.from.id;
					                    }

					                    if(!nodeIsExist[item.to.id]){
					                    	topoData.nodes.push({
						                        data: {
						                            id: item.to.id,
						                            name: item.to.name
						                        }
						                    });
						                    item.to.isNeed = true;
						                    nodeIsExist[item.to.id] = item.to.id;
					                    }
						    		}
							    }
						    }
						});
						data.relations.forEach(function(item) {
							if(item.from&&item.to){
								if(nodeIsExist[item.from.id]){
									item.from.isNeed = true;
								}
								if(nodeIsExist[item.to.id]){
									item.to.isNeed = true;
								}
							}
						});
					}

					data.relations.forEach(function(item) {
					    if(item.from){
			                delete item.from.isNeed;
					    }
					    if(item.to){
					    	delete item.to.isNeed;
					    }
					});
				}



				var cy = cytoscape({
				    container: document.getElementById('topoServer'),
				    style: cytoscape.stylesheet()
				        .selector('node')
				        .css({
				            'width': 16,
				            'height': 16,
				            'background-color': '#53cdfc',
				            'color': '#ffffff',
				            'shape': 'ellipse',
				            'content':'data(name)',
				            'font-size':'12px',
				            'font-family': 'Times New Roman'
				        })
				        .selector('edge')
				        .css({
				            'width': 1,
				            'target-arrow-shape': 'triangle',
				            'opacity': 1,
				            'curve-style': 'bezier',
				            'line-color': '#53cdfc',
				            'target-arrow-color':'#53cdfc'
				        })
				        .selector(':selected')
				        .css({
				            'background-color': 'black',
				            'line-color': 'black',
				            'target-arrow-color': 'black',
				            'source-arrow-color': 'black',
				            'opacity': 1
				        })
				        .selector('.faded')
				        .css({
				            'opacity': 0.25,
				            'text-opacity': 0
				        }),

				    elements: {
				        nodes: topoData.nodes, 
				        edges: topoData.edges
				    },

				    layout: {
				        name: 'cose'
				    },

				    minZoom: 0.1,
				    maxZoom: 4,
				    wheelSensitivity: 0.4
				});
				cycleIsEnd = 1;
			},
			topoServerRender: function(){
				var ctrl = this;
				var data = this.get('model');
				var timer = setInterval(function(){
					if($('#topoServer').length){
						var winHeight = $(window).height();
						$('#topoServer').height(winHeight-200);
						console.time('1111')  
						ctrl.topoDataRender(data);
    					console.timeEnd('1111') 

						$(window).on('resize',function(){
							var winHeight = $(window).height();
							$('#topoServer').height(winHeight-200);
							console.time('22')  
							ctrl.topoDataRender(data);
	    					console.timeEnd('22') 
						});
						clearInterval(timer);
					}
				},100);
			}
		});
	
		APM.ServiceServerToposerverController = Controller;

		return Controller;
	}
);	